package cn.exrick.xboot.modules.base.controller.manage;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.base.entity.Department;
import cn.exrick.xboot.modules.base.entity.Organization;
import cn.exrick.xboot.modules.base.param.OrganizationParam;
import cn.exrick.xboot.modules.base.service.mybatis.IOrganizationService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * njp 20201105
 * 暂时不使用这个做为系统的组织
 * 因部门与权限逻辑完整直接采用部门作为系统的组织结构
 * @author njp
 */
@Slf4j
@RestController
@Api(description = "组织机构管理接口")
@RequestMapping("/xboot/organization")
@Transactional
public class OrganizationController {

    @Autowired
    private IOrganizationService iOrganizationService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Organization> get(@PathVariable String id){

        Organization organization = iOrganizationService.getById(id);
        return new ResultUtil<Organization>().setData(organization);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Organization>> getAll(){

        List<Organization> list = iOrganizationService.list();
        return new ResultUtil<List<Organization>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Organization>> getByPage(@ModelAttribute PageVo page){

        IPage<Organization> data = iOrganizationService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<Organization>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Organization> saveOrUpdate(@ModelAttribute Organization organization){

        if(iOrganizationService.saveOrUpdate(organization)){
            return new ResultUtil<Organization>().setData(organization);
        }
        return new ResultUtil<Organization>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){
        for(String id : ids){
            OrganizationParam organizationParam = new OrganizationParam();
            organizationParam.setId(id);
            iOrganizationService.delete(organizationParam);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getByParentId/{parentId}",method = RequestMethod.GET)
    @ApiOperation(value = "通过当前id获取当前节点的子集")
    public Result<List<Organization>> getByParentId(@PathVariable String parentId,
                                                  @ApiParam("是否开始数据权限过滤") @RequestParam(required = false, defaultValue = "true") Boolean openDataFilter){
        OrganizationParam organizationParam = new OrganizationParam();
        organizationParam.setId(parentId);
        //todo 添加缓存
        return new ResultUtil<List<Organization>>().setData(iOrganizationService.getByParentId(organizationParam));
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加")
    public Result<Object> add(@Validated(OrganizationParam.add.class) @ModelAttribute OrganizationParam organizationParam){
        //todo 更新缓存（也就是删除缓存）
        iOrganizationService.add(organizationParam);
        return new ResultUtil<Object>().setSuccessMsg("添加成功");
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ApiOperation(value = "编辑")
    public Result<Object> edit(@ModelAttribute OrganizationParam organizationParam) {
    //1.todo 更改数据
        iOrganizationService.edit(organizationParam);
    //2.todo 更新缓存（也就是删除缓存）
        return new ResultUtil<Object>().setSuccessMsg("修改成功");
    }

    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Organization> getById(@Param("id") String id){
        Organization org = iOrganizationService.getById(id);
        if(ObjectUtil.isNotEmpty(org)){
            return new ResultUtil<Organization>().setData(org);
        }
        return new ResultUtil<Organization>().setErrorMsg("数据不存在");
    }

    @RequestMapping(value = "/search",method = RequestMethod.GET)
    @ApiOperation(value = "组织名模糊搜索")
    public Result<List< Organization>> searchByTitle(@RequestParam String title){
        List<Organization> orgList=null;
        //为空条件设置默认值
        if(ObjectUtil.isNull(title)||"".equals(title)){
            OrganizationParam organizationParam = new OrganizationParam();
            organizationParam.setId("0");
            orgList = iOrganizationService.getByParentId(organizationParam);
            return new ResultUtil<List< Organization>>().setData(orgList);
        }
        //模糊查询 todo
        orgList=iOrganizationService.search(title);
        return new ResultUtil<List< Organization>>().setData(orgList);
    }


    }
